<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('op:init:hotel')" type="danger" @click="hotelInitHandleMT()">美团酒店初始化</el-button>
        <el-button v-if="isAuth('op:init:hotel')" type="danger" @click="hotelInitHandleXC()">携程酒店初始化</el-button>
        <!-- <el-button v-if="isAuth('op:init:hotel')" type="danger" @click="redisInitHandle()">Redis初始化</el-button> -->
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column prop="excuTime" header-align="center" align="center" label="开始执行时间"></el-table-column>
      <el-table-column prop="taskName" header-align="center" align="center" label="任务名称"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === 0 && scope.row.pause != -1"
            size="small"
            type="success"
          >执行中</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="small" type="success">已完成</el-tag>
          <el-tag v-else size="small" type="info">暂停</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="compPercent" header-align="center" align="center" label="完成数"></el-table-column>
      <el-table-column prop="compAmount" header-align="center" align="center" label="总数"></el-table-column>
      <el-table-column
        prop="compPercent"
        :formatter="tranCompPercent"
        header-align="center"
        align="center"
        label="完成百分比"
      ></el-table-column>
      <el-table-column prop="compTime" header-align="center" align="center" label="完成时间"></el-table-column>
      <el-table-column prop="archive" header-align="center" align="center" label="归档标志">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.archive === 1" size="small" type="info">历史</el-tag>
          <el-tag v-else size="small" type="success">当前</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="100" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="scope.row.taskName ==='全量酒店拉取' && scope.row.pause !=-1 && scope.row.status ===0"
            @click="pause(scope.row.id,-1)"
          >暂停</el-button>
          <el-button
            type="text"
            size="small"
            v-if="scope.row.taskName ==='全量酒店拉取' && scope.row.pause === -1 && scope.row.status ===0"
            @click="pause(scope.row.id,0)"
          >继续拉取</el-button>
          <el-button type="text" size="small" @click="viewDetal(scope.row)">异常详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>

export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/op/taskstatus/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'taskCode': 'ALLHOTEL'
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 酒店初始化
    hotelInitHandleMT() {
      this.$confirm('确定拉取所有酒店信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/op/taskstatus/init/hotel/mt'),
          method: 'get'
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '开始拉取...',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })

      }).catch(() => {

      })
    },

    // 携程酒店拉取
    hotelInitHandleXC() {
      this.$confirm('确定拉取所有酒店信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/op/taskstatus/init/hotel/xc'),
          method: 'get'
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '开始拉取...',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })

      }).catch(() => {

      })
    },
    //redis初始化
    redisInitHandle() {
      this.$confirm('确定拉初始化Redis增量更新RP信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/op/dtuphtl/redis/init'),
          method: 'get'
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '初始化完成',
              type: 'success',
              duration: 1000,
              onClose: () => {
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })

      }).catch(() => {
      })
    },
    tranCompPercent(row, column) {
      let percent = ""
      if (row.compAmount === null || row.compAmount === undefined) {
        percent = 0 + "%"
      } else if (row.compAmount != null && row.compAmount != undefined && row.compAmount != 0) {
        let tem = (row.compPercent / row.compAmount) * 100 + ""
        let re = /([0-9]+\.[0-9]{2})[0-9]*/
        percent = tem.replace(re, "$1") + "%"
      } else if (row.status === 1 || row.compAmount === 0) {
        percent = "100%"
      } else {
        let tem = (row.compAmount / 380000) * 100
        let re = /([0-9]+\.[0-9]{2})[0-9]*/ + ""
        percent = tem.replace(re, "$1") + "%"
      }
      return percent
    },
    viewDetal(taskInfo) {
      this.$router.push({ name: 'op-taskdetail', params: { taskInfo: taskInfo } })
    },
    //暂停，继续拉取全量酒店
    pause(id, pause) {
      this.$confirm(`确定${pause ? '暂停' : '继续'}拉取全量酒店吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/op/taskstatus/init/hotel/pause'),
          method: 'get',
          params: this.$http.adornParams({
            'id': id,
            'pause': pause
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '开始拉取...',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })

      }).catch(() => {

      })
    }
  }
}
</script>
